<html>
<head><meta charset="utf-8"><title>Why does `InferCtxt` use interior mutability? · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60InferCtxt.60.20use.20interior.20mutability.3F.html">Why does `InferCtxt` use interior mutability?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="223829775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60InferCtxt%60%20use%20interior%20mutability%3F/near/223829775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60InferCtxt.60.20use.20interior.20mutability.3F.html#223829775">(Jan 24 2021 at 19:11)</a>:</h4>
<p>I just tried to make the following refactor to rustdoc:</p>
<div class="codehilite" data-code-language="Diff"><pre><span></span><code><span class="gh">diff --git a/src/librustdoc/clean/blanket_impl.rs b/src/librustdoc/clean/blanket_impl.rs</span>
<span class="gh">index c4a2050a818..aaa43122c97 100644</span>
<span class="gd">--- a/src/librustdoc/clean/blanket_impl.rs</span>
<span class="gi">+++ b/src/librustdoc/clean/blanket_impl.rs</span>
<span class="gu">@@ -24,6 +24,10 @@ impl&lt;'a, 'tcx&gt; BlanketImplFinder&lt;'a, 'tcx&gt; {</span>
         let param_env = self.cx.tcx.param_env(item_def_id);
         let ty = self.cx.tcx.type_of(item_def_id);

<span class="gi">+        let substs = infcx.fresh_substs_for_item(DUMMY_SP, item_def_id);</span>
<span class="gi">+        let ty = ty.subst(infcx.tcx, substs);</span>
<span class="gi">+        let param_env = param_env.subst(infcx.tcx, substs);</span>
<span class="gi">+</span>
         debug!("get_blanket_impls({:?})", ty);
         let mut impls = Vec::new();
         for &amp;trait_def_id in self.cx.tcx.all_traits(LOCAL_CRATE).iter() {
<span class="gu">@@ -44,10 +48,6 @@ impl&lt;'a, 'tcx&gt; BlanketImplFinder&lt;'a, 'tcx&gt; {</span>
                         _ =&gt; return false,
                     }

<span class="gd">-                    let substs = infcx.fresh_substs_for_item(DUMMY_SP, item_def_id);</span>
<span class="gd">-                    let ty = ty.subst(infcx.tcx, substs);</span>
<span class="gd">-                    let param_env = param_env.subst(infcx.tcx, substs);</span>
<span class="gd">-</span>
                     let impl_substs = infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id);
                     let trait_ref = trait_ref.subst(infcx.tcx, impl_substs);
</code></pre></div>
<p>which broke because it depends on the infer context. I'm worried about moving the infer context out of the inner loop because it uses interior mutability and I don't know if it will have the same behavior if I only call <code>infer_ctxt().enter(|| ...)</code> once. Why does it use interior mutability?</p>



<a name="223839434"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60InferCtxt%60%20use%20interior%20mutability%3F/near/223839434" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60InferCtxt.60.20use.20interior.20mutability.3F.html#223839434">(Jan 24 2021 at 22:27)</a>:</h4>
<p>well I just spent entirely too much time finding out why, but I think it might be possible to get rid of <a href="https://github.com/jyn514/rust/tree/infer-mut/">https://github.com/jyn514/rust/tree/infer-mut/</a></p>



<a name="223839439"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60InferCtxt%60%20use%20interior%20mutability%3F/near/223839439" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60InferCtxt.60.20use.20interior.20mutability.3F.html#223839439">(Jan 24 2021 at 22:27)</a>:</h4>
<p>about 22 errors left, I started with ~100</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>